<template>
  <div class="comprehensive-table-container">
    <vab-query-form>
      <vab-query-form-top-panel>
        <el-form
          :inline="true"
          label-width="300px"
          :model="queryForm"
          @submit.prevent
        >
          <el-form-item label="请输入用户信息以筛选">
            <el-input
              v-model="queryForm.title"
              placeholder="双方姓名或订单号"
            />
          </el-form-item>

          <el-form-item>
            <el-button
              icon="el-icon-search"
              native-type="submit"
              style="
                background-color: #58b5e1;
                color: #fff;
                border: 0px;
                font-size: 14px;
              "
              @click="queryData"
            >
              查询
            </el-button>
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="queryForm.type"
              placeholder="资金类型"
              style="margin-left: 10px"
              @change="queryData"
            >
              <el-option label="全部类型" value="" />
              <el-option label="捐款" :value="0" />
              <el-option label="捐物资" :value="1" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="queryForm.status"
              placeholder="审核状态"
              style="margin-left: 10px"
              @change="queryData"
            >
              <el-option label="全部状态" value="" />
              <el-option label="待审核" :value="0" />
              <el-option label="已通过" :value="1" />
              <el-option label="已驳回" :value="2" />
            </el-select>
          </el-form-item>
        </el-form>
      </vab-query-form-top-panel>
    </vab-query-form>

    <el-table
      ref="tableSortRef"
      v-loading="listLoading"
      border
      :cell-style="{ padding: '4px 0 4px 0' }"
      :data="list"
      :header-cell-style="{
        background: '#E1EAF3 !important',
        'text-align': 'center',
      }"
      :height="500"
      :row-style="{ background: '#E4F2FF' }"
      @selection-change="setSelectRows"
      @sort-change="tableSortChange"
    >
      <el-table-column
        align="center"
        show-overflow-tooltip
        type="selection"
        width="55"
      />
      <el-table-column
        align="center"
        label="序号"
        show-overflow-tooltip
        width="55"
      >
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="发起人"
        prop="uHandleName"
        show-overflow-tooltip
      />
      <el-table-column align="center" label="类型" show-overflow-tooltip>
        <template #default="{ row }">
          <el-tag v-if="row.aidType == 0" type="warning">捐款</el-tag>
          <el-tag v-else type="success">捐物资</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="目标用户"
        prop="uResponseName"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="捐赠内容"
        prop="money"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <B v-if="row.aidType == 0" style="color: #0dbc79">
            {{ '￥' + row.money }}
          </B>
          <span v-else>物资</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="完成状态"
        prop="uTargetName"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <el-tag v-if="row.status == 0" type="warning">待处理</el-tag>
          <el-tag v-if="row.status == 1" type="success">成功</el-tag>
          <el-tag
            v-if="row.status == 2"
            style="
              background-color: #fa6567;
              color: #fff;
              border: 0px;
              font-size: 14px;
            "
          >
            驳回
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="发起时间"
        prop="createTime"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        label="物流号 / 流水号"
        prop="oddNumber"
        show-overflow-tooltip
        width="160"
      >
        <template #default="{ row }">
          {{ row.oddNumber ? row.oddNumber : '/' }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        show-overflow-tooltip
        width="80"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleDetail(row)">详情</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty class="vab-data-empty" description="暂无数据" />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :page-sizes="[30, 40, 50, 100]"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <table-edit ref="editRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getList } from '@/api/tracking'
  import {
    defineComponent,
    getCurrentInstance,
    onBeforeMount,
    onBeforeUnmount,
    onMounted,
    reactive,
    toRefs,
    watch,
  } from 'vue'
  import { useRouter, useRoute } from 'vue-router'
  import TableEdit from './components/TableEdit'

  export default defineComponent({
    name: 'ComprehensiveTable',
    components: {
      TableEdit,
    },
    setup() {
      const router = useRouter()
      const route = useRoute()
      const { proxy } = getCurrentInstance()

      const state = reactive({
        editRef: null,
        tableSortRef: null,
        fold: false,
        height: proxy.$baseTableHeight(2) - 30,
        imgShow: true,
        list: [],
        imageList: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        showEditService: false,
        service_tel: [],
        queryForm: {
          pageNo: 1,
          pageSize: 30,
        },
      })

      watch(
        () => route.path,
        (value) => {
          if (value === '/system/admin/comprehensiveTable') fetchData()
        }
      )

      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { records: list, total },
        } = await getList(state.queryForm)
        state.list = list
        const imageList = []
        list.forEach((item) => {
          console.log(item.avatarUrl)
          imageList.push(item.avatarUrl)
        })
        state.imageList = imageList
        state.total = total
        state.listLoading = false
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const handleHeight = () => {
        if (state.fold) state.height = proxy.$baseTableHeight(2) - 47
        else state.height = proxy.$baseTableHeight(3) - 30
      }
      const tableSortChange = () => {
        const imageList = []
        state['tableSortRef'].tableData.forEach((item) => {
          imageList.push(item.img)
        })
        this.imageList = imageList
      }
      const setSelectRows = (val) => {
        state.selectRows = val
      }

      const handleDetail = (row) => {
        if (row.id)
          router.push({
            path: '/tracking/detail',
            query: row,
          })
        else {
          proxy.$baseMessage(
            '请选择一行进行详情页跳转',
            'error',
            'vab-hey-message-error'
          )
        }
      }

      onBeforeMount(() => {
        window.addEventListener('resize', handleHeight)
      })
      onBeforeUnmount(() => {
        window.removeEventListener('resize', handleHeight)
      })
      onMounted(() => {
        fetchData()
      })

      return {
        ...toRefs(state),
        handleSizeChange,
        handleCurrentChange,
        handleHeight,
        tableSortChange,
        setSelectRows,
        handleDetail,
        fetchData,
        queryData,
      }
    },
    methods: {},
  })
</script>
